﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace Sublimation
{
    public abstract class MenuItem : GameObject
    {
        protected float rotationAngle = 0.0f;
        protected float deltaRotation = 0.0f;
        public float DeltaRotation 
        {
            set { deltaRotation = value; }
            get { return deltaRotation; }
        }
        protected Vector2 center;
        public Vector2 Center
        {
            set { center = value; }
            get { return center; }
        }

        protected MenuItemStatus itemStatus;
        protected Texture2D towerbottom;
        public Texture2D TowerButtom
        {
            set { towerbottom = value; }
            get { return towerbottom;}
        }
        protected Texture2D towertop;
        protected Texture2D addItem;
        public Texture2D AddItem
        {
            set { addItem = value; }
            get { return addItem; }
        }
        protected Texture2D unbuiltTower;
        public Texture2D UnbuiltTower
        {
            set { unbuiltTower = value; }
            get { return unbuiltTower; }
        }
        protected Texture2D builtTower;
        public Texture2D SelectTower
        {
            set { selectTower = value; }
            get { return selectTower; }
        }
        protected Texture2D selectTower;
        protected Color towerColor;
        protected GameObject parent;


        public MenuItem(WorldProperties worldProperties, GameObject parent)
            : base(worldProperties)
        {
            this.parent = parent;
            Scale = 1.0f;
            towerColor = Color.White;
            SpriteColor = Color.White;
            DrawOrder = 101;
        }

        public bool IntersectTower(Tower aTower)
        {
            Vector2 itemCenter = new Vector2(this.Position.X + this.Sprite.Width / 2, this.Position.Y + this.Sprite.Height / 2);
            float distance = Vector2.Distance(aTower.Position, itemCenter);
            
            if (distance < 2 * Tower.RADIUS)
            {
                return true;
            }
            else
                return false;
         }

        public void Translate(Vector2 translateVector)
        {
            Position = new Vector2(Position.X + translateVector.X, Position.Y + translateVector.Y);
            Center = new Vector2(Center.X + translateVector.X, Center.Y + translateVector.Y);
        }

        public void Rotate(float rotation)
        {
            // if the item is not being dragged now
            if (ItemStatus != MenuItemStatus.OnMove)
            {
                Position = RotatePoint(parent.Position, rotation, Position);
                Center = RotatePoint(parent.Position, rotation, Center);
                DeltaRotation = rotation;
                Rotation += rotation;
            }
        }

        #region GameObject implementation

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }

        protected override void LoadContent()
        {
            base.LoadContent();
            addItem = Game.Content.Load<Texture2D>("Sprites/add_a_tower");
            unbuiltTower = Game.Content.Load<Texture2D>("Sprites/unavailable_icon");
            builtTower = Game.Content.Load<Texture2D>("Sprites/tower_been_built");
            selectTower = Game.Content.Load<Texture2D>("Sprites/select_a_tower");
            towerbottom = Game.Content.Load<Texture2D>("Sprites/t_basement");
            towertop = Game.Content.Load<Texture2D>("Sprites/t_rotate");
            sprite = unbuiltTower;
        }


        public override void Draw(GameTime gameTime)
        {
            if (Sprite != null)
            {
                spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
                // if the item is being dragged
                if (this.ItemStatus == MenuItemStatus.OnMove)
                {
                    sprite = towerbottom;
                    // make it transparent
                    this.SpriteColor = new Color(255, 255, 255, 0.3f);
                    this.TowerColor = new Color(255, 255, 255, 0.3f);
                    spriteBatch.Draw(towertop, Position, null, towerColor, Rotation, Origin, 1.0f, SpriteEffects.None, 1.0f);
                }
       
                spriteBatch.End();
            }

            base.Draw(gameTime);
        }
        #endregion

        public GameObject Parent
        {
            get { return parent; }
            set { parent = value; }
        }

        public Color TowerColor
        {
            get { return towerColor; }
            set { towerColor = value; }
        }

        public MenuItemStatus ItemStatus
        {
            get { return itemStatus; }
            set { itemStatus = value; }
        }

        public float RotationAngle
        {
            get { return rotationAngle; }
            set { rotationAngle = value; }
        }


        
    }
}

